package com.chenyun.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.Assert;
import com.chenyun.boot.datasource.page.PageUtils;
import com.chenyun.common.annotation.Log;
import com.chenyun.common.enums.operlog.BusinessTypeEnum;
import com.chenyun.common.vo.AjaxResult;
import com.chenyun.common.vo.PageResult;
import com.chenyun.common.vo.QueryPage;
import com.chenyun.system.model.SmsChannel;
import com.chenyun.system.model.SmsTemplate;
import com.chenyun.system.param.SmsTemplateQueryPage;
import com.chenyun.system.service.ISmsChannelService;
import com.chenyun.system.service.ISmsTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 短信渠道设置
 *
 * @author: Lonni
 * @date: 2022/12/27 0027 19:14
 */
@Api(tags = "短信模板设置-admin")
@RestController
@RequestMapping("sms/template")
public class SmsTemplateController {

    private ISmsTemplateService templateService;

    @ApiOperation("获取模板列表")
    @PostMapping("list")
    @SaCheckPermission("sms:template:list")
    public PageResult list(@RequestBody SmsTemplateQueryPage queryPage) {
        return templateService.page(queryPage);
    }

    @ApiOperation("添加模板")
    @PostMapping("add")
    @Log(title = "添加短信模版", businessType = BusinessTypeEnum.INSERT)
    @SaCheckPermission("sms:template:add")
    public AjaxResult<SmsTemplate> addSmsChannel(@RequestBody SmsTemplate template) {
        return AjaxResult.SUCCESS(templateService.createTemplate(template));
    }

    @ApiOperation("修改模板")
    @PostMapping("edit")
    @Log(title = "修改短信模版", businessType = BusinessTypeEnum.UPDATE)
    @SaCheckPermission("sms:template:edit")
    public AjaxResult<Boolean> editSmsChannel(@RequestBody SmsTemplate template) {
        return AjaxResult.SUCCESS(templateService.updatetemplate(template));
    }

    @ApiOperation("删除短信模板")
    @PostMapping("del/{id}")
    @Log(title = "删除短信模版", businessType = BusinessTypeEnum.DELETE)
    @SaCheckPermission("sms:template:del")
    public AjaxResult<Boolean> delTemplate(@PathVariable Long id) {
        Assert.notNull(id, "模板编号必输!");
        return AjaxResult.SUCCESS(templateService.delTemplate(id));
    }

    @ApiOperation("获取指定短信渠道下模版数量")
    @PostMapping("temCount/{id}")
    @ApiImplicitParam(name = "id", value = "短信渠道的id值")
    @SaCheckPermission("sms:template:temCount")
    public AjaxResult<Long> addSmsChannel(@PathVariable Long id) {
        return AjaxResult.SUCCESS(templateService.getTemplateCountByChannel(id));
    }
}



